package core

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/columnd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/evtop"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/factory"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/groupd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/interactiond"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/oplogd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/orgd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/reportd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/reviewd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 主域id
	DomainID = "cms"
)

// 创建一个主内容信息发布系统领域
func newMainDomain() CmsDomain {
	md := &_domain{}
	md.Init(md, nil, DomainID)
	return md
}

// Option 领域初始化参数
type Option struct {
	Infod       infod.DomainOption
	OpLog       oplogd.DomainOption
	Column      columnd.DomainOption
	Group       groupd.DomainOption
	Interaction interactiond.DomainOption
	Org         orgd.DomainOption
	Report      reportd.DomainOption
	Review      reviewd.DomainOption
}

// Reg 注册一个信息发布系统领域
func Reg(opt Option) CmsDomain {
	d := newMainDomain()
	// 注册主领域
	ddd.RegDomain(d)
	oplogd.NewOpLogDomain(d, opt.OpLog)
	infod.NewDomain(d, opt.Infod)
	columnd.NewDomain(d, opt.Column)
	groupd.NewDomain(d, opt.Group)
	interactiond.NewDomain(d, opt.Interaction)
	orgd.NewDomain(d, opt.Org)
	reportd.NewDomain(d, opt.Report)
	reviewd.NewDomain(d, opt.Review)

	ddd.PrintDomain(d)
	evtop.Init(d)
	factory.Init(d)
	return d
}

// CmsDomain 信息发布系统领域
type CmsDomain interface {
	ddd.Domain
	// 获取信息核心子域
	Info() infod.InfoDomain
	// 获取操作日志通用子域
	OpLog() oplogd.OpLogDomain
	// 栏目子域
	Column() columnd.ColumnDomain
	Group() groupd.GroupDomain
	Interaction() interactiond.InteractionDomain
	Org() orgd.OrgDomain
	Report() reportd.ReportDomain
	Review() reviewd.ReviewDomain
}

type _domain struct {
	ddd.BaseDomain
	Infod        infod.InfoDomain
	OpLogd       oplogd.OpLogDomain
	Columnd      columnd.ColumnDomain
	Groupd       groupd.GroupDomain
	Interactiond interactiond.InteractionDomain
	Orgd         orgd.OrgDomain
	Reportd      reportd.ReportDomain
	Reviewd      reviewd.ReviewDomain
}

func (d *_domain) Info() infod.InfoDomain {
	if d.Infod != nil {
		return d.Infod
	}
	r := d.BaseDomain.SubDomain(infod.DomainID)
	if r == nil {
		panic("没有初始化infod.InfoDomain")
	}
	ir, ok := r.(infod.InfoDomain)
	if !ok {
		panic("初始化infod.InfoDomain，类型错误")
	}
	d.Infod = ir
	return ir
}
func (d *_domain) OpLog() oplogd.OpLogDomain {
	if d.OpLogd != nil {
		return d.OpLogd
	}

	r := d.BaseDomain.SubDomain(oplogd.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化oplogd.OpLogDomain")
	}
	ir, ok := r.(oplogd.OpLogDomain)
	if !ok {
		panic("初始化oplogd.OpLogDomain，类型错误")
	}
	d.OpLogd = ir
	return ir
}
func (d *_domain) Column() columnd.ColumnDomain {
	if d.Columnd != nil {
		return d.Columnd
	}

	r := d.BaseDomain.SubDomain(columnd.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化columnd.ColumnDomain")
	}
	ir, ok := r.(columnd.ColumnDomain)
	if !ok {
		panic("初始化columnd.ColumnDomain，类型错误")
	}
	d.Columnd = ir
	return ir
}
func (d *_domain) Group() groupd.GroupDomain {
	if d.Groupd != nil {
		return d.Groupd
	}

	r := d.BaseDomain.SubDomain(groupd.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化groupd.GroupDomain")
	}
	ir, ok := r.(groupd.GroupDomain)
	if !ok {
		panic("初始化groupd.GroupDomain，类型错误")
	}
	d.Groupd = ir
	return ir
}
func (d *_domain) Interaction() interactiond.InteractionDomain {
	if d.Interactiond != nil {
		return d.Interactiond
	}
	r := d.BaseDomain.SubDomain(interactiond.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化 interactiond.InteractionDomain")
	}
	ir, ok := r.(interactiond.InteractionDomain)
	if !ok {
		panic("初始化 interactiond.InteractionDomain，类型错误")
	}
	d.Interactiond = ir
	return ir
}
func (d *_domain) Org() orgd.OrgDomain {
	if d.Orgd != nil {
		return d.Orgd
	}
	r := d.BaseDomain.SubDomain(orgd.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化 orgd.OrgDomain")
	}
	ir, ok := r.(orgd.OrgDomain)
	if !ok {
		panic("初始化 orgd.OrgDomain，类型错误")
	}
	d.Orgd = ir
	return ir
}
func (d *_domain) Report() reportd.ReportDomain {
	if d.Reportd != nil {
		return d.Reportd
	}
	r := d.BaseDomain.SubDomain(reportd.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化 reportd.ReportDomain")
	}
	ir, ok := r.(reportd.ReportDomain)
	if !ok {
		panic("初始化 reportd.ReportDomain，类型错误")
	}
	d.Reportd = ir
	return ir
}
func (d *_domain) Review() reviewd.ReviewDomain {
	if d.Reviewd != nil {
		return d.Reviewd
	}
	r := d.BaseDomain.SubDomain(reviewd.DomainID)
	if r == nil {
		ddd.PrintDomain(d)
		panic("没有初始化 reviewd.ReviewDomain")
	}
	ir, ok := r.(reviewd.ReviewDomain)
	if !ok {
		panic("初始化 reviewd.ReviewDomain，类型错误")
	}
	d.Reviewd = ir
	return ir
}
